//
//  HWWuKongCell.swift
//  HWanGame
//
//  Created by Apple on 2024/8/6.
//  Copyright © 2024 94Wan. All rights reserved.
//

import UIKit
import Stevia

//MARK: 资讯cell
class HWWuKongInfoCell: UITableViewCell {

    private let backView = c_make_ex(UIView.self)
        .bgColor(.white).corner(12*widthScale).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("2E3033").font(15.font_medium).line(2).end()
    private let iconImageView = c_make_ex(UIImageView.self)
        .corner(8).mode(.scaleAspectFill).end()
    private let browseImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("WuKong_look").end()
    private let commitImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("WuKong_commit").end()
    let likeImageView = c_make_ex(UIButton.self)
        .image("WuKong_like").image("Detail_like_sel", state: .selected).end()
    private let browseLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(10.font_regular).text("999浏览").end()
    private let commitLikeLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(10.font_regular).alignment(.right).end()
    
    var likeBlock: ((_ infoID: String) -> Void)?
    
    var model: HWWuKongDataModel?{
        didSet{
            guard let md = model?.data else { return }
            
            titleLabel.text = md.title
            iconImageView.sd_setImage(with: URL(string: md.image))
            browseLabel.text = md.look + "浏览"
            commitLikeLabel.text = md.pinglun + "评论 · " + md.zan + "点赞  "
            likeImageView.isSelected = md.is_zan
        }
    }
    
    var infoModel: HWHomeInfoItemModel?{
        didSet{
            guard let md = infoModel else { return }
            
            iconImageView.sd_setImage(with: URL(string: md.image))
            titleLabel.text = md.title
            browseLabel.text = md.look + "浏览"
            commitLikeLabel.text = md.pinglun + "评论 · " + md.zan + "点赞  "
            likeImageView.isSelected = md.is_zan == 1
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            backView.subviews{
                titleLabel
                iconImageView
                browseImageView
                commitImageView
                likeImageView
                browseLabel
                commitLikeLabel
            }
        }
                
        backView.bottom(12*widthScale).leading(12*widthScale).trailing(12*widthScale).top(0)
        titleLabel.leading(16*widthScale).trailing(16*widthScale).top(16*widthScale)
        iconImageView.leading(16*widthScale).trailing(16*widthScale).height(155*widthScale)
        iconImageView.Top == titleLabel.Bottom + 8*widthScale
        browseImageView.leading(16*widthScale).size(44*widthScale)
        browseImageView.Top == iconImageView.Bottom
        likeImageView.trailing(16*widthScale).size(44*widthScale)
        likeImageView.CenterY == browseImageView.CenterY
        commitImageView.size(44*widthScale)
        commitImageView.CenterY == likeImageView.CenterY
        commitImageView.Trailing == likeImageView.Leading
        browseLabel.leading(26*widthScale).bottom(15*widthScale)
        browseLabel.Top == browseImageView.Bottom
        commitLikeLabel.trailing(26*widthScale)
        commitLikeLabel.CenterY == browseLabel.CenterY
        
        likeImageView.addTarget(self, action: #selector(clickLikeButton), for: .touchUpInside)
    }
    
    @objc private func clickLikeButton(){
        
        if let infoID = model?.data?.id {
            likeBlock?(infoID)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

//MARK: 动态cell
class HWWuKongDynamicCell: UITableViewCell {

    private let backView = c_make_ex(UIView.self)
        .bgColor(.white).corner(12*widthScale).end()
    private let iconImageView = c_make_ex(UIImageView.self)
        .corner(20).mode(.scaleAspectFill).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#17181A").font(12.font_semiBold).end()
    private let subLabel = c_make_ex(UILabel.self)
        .color("#AEB0B3").font(11.font_medium).end()
    private let attentionButton = UIIconButton(style: .leading(itemSpace: 2))
    private let moreButton = c_make_ex(UIButton.self)
        .bgColor(.red).end()
    private let contentLabel = c_make_ex(UILabel.self)
        .color("#47494D").font(15.font_medium).line(0).end()
    private let commitImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("WuKong_commit").end()
    private let likeImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("WuKong_like").end()
    private let commitLikeLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(10.font_regular).alignment(.right)
        .text("999评论 · 999点赞").end()
    
    var model: HWWuKongDataModel?{
        didSet{
            guard let md = model?.data else { return }
            
            nameLabel.text = md.pet_name
            iconImageView.sd_setImage(with: URL(string: md.face))
            contentLabel.text = md.content
            commitLikeLabel.text = md.pinglun + "评论 · " + md.zan + "点赞  "
//            browseLabel.text = md.look + "浏览"
//            commitLikeLabel.text = md.pinglun + "评论 · " + md.zan + "点赞  "
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            backView.subviews{
                nameLabel
                iconImageView
                subLabel
                attentionButton
                moreButton
                contentLabel
                commitImageView
                likeImageView
                commitLikeLabel
            }
        }
                
        backView.bottom(12*widthScale).leading(12*widthScale).trailing(12*widthScale).top(0)
        iconImageView.leading(12*widthScale).top(16*widthScale).size(40*widthScale)
        nameLabel.leading(65*widthScale).trailing(120*widthScale).top(22*widthScale)
        moreButton.trailing(8*widthScale).size(32*widthScale).top(20*widthScale)
        attentionButton.height(26*widthScale).width(70*widthScale)
        attentionButton.CenterY == moreButton.CenterY
        attentionButton.Leading == moreButton.Trailing - 4*widthScale
        subLabel.leading(65*widthScale).top(40*widthScale)
        contentLabel.leading(65*widthScale).trailing(16*widthScale)
        contentLabel.Top == iconImageView.Bottom + 8*widthScale
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}


//MARK: 装备资料cell
class HWWuKongEquipCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    var news: [HWStrategyBlockItemModel]?{
        didSet{
            collectionView.reloadData()
        }
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        subviews{
            collectionView
        }
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 8
        layout.minimumInteritemSpacing = 8
        layout.scrollDirection = .horizontal
        layout.sectionInset = UIEdgeInsets(top: 0, left: 12*widthScale, bottom: 0, right: 0)
        
        collectionView.collectionViewLayout = layout
        collectionView.backgroundColor = .clear
        collectionView.register(HWWuKongSlideCateCell.self, forCellWithReuseIdentifier: HWWuKongSlideCateCell.description())
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
        
        collectionView.leading(0).trailing(0).bottom(0).top(10*widthScale)
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return news?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HWWuKongSlideCateCell.description(), for: indexPath) as? HWWuKongSlideCateCell else { return UICollectionViewCell() }
        cell.model = news?[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let infoID = news?[indexPath.row].news_id ?? 0
        let dict = ["infoID": infoID]
        NotificationCenter.default.post(name: .toInfoDetailVC, object: nil, userInfo: dict)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let name = news?[indexPath.row].title ?? ""
        let font = 12.font_regular
        let attributedText = NSAttributedString(string: name, attributes: [NSAttributedString.Key.font: font as Any])

        // 计算文本宽度
        let size = attributedText.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, context: nil).size
        let width = size.width + 24*widthScale
        
        return CGSize(width: width, height: 28.0*widthScale)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}


//MARK: 主线图⽂攻略cell
class HWStrategyImageTextCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {

    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    var news: [HWStrategyBlockItemModel]?{
        didSet{
            collectionView.reloadData()
        }
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        subviews{
            collectionView
        }
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 12
        layout.minimumInteritemSpacing = 12
        layout.scrollDirection = .horizontal
        layout.sectionInset = UIEdgeInsets(top: 0, left: 12*widthScale, bottom: 0, right: 0)
        layout.itemSize = CGSize(width: 120*widthScale, height: 175*widthScale)
        
        collectionView.collectionViewLayout = layout
        collectionView.backgroundColor = .clear
        collectionView.register(HWWuKongImageTextStrategyCell.self, forCellWithReuseIdentifier: HWWuKongImageTextStrategyCell.description())
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
        
        collectionView.leading(0).trailing(0).bottom(0).top(10*widthScale)
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return news?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HWWuKongImageTextStrategyCell.description(), for: indexPath) as? HWWuKongImageTextStrategyCell else { return UICollectionViewCell() }
        cell.model = news?[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let infoID = news?[indexPath.row].news_id ?? 0
        let dict = ["infoID": infoID]
        NotificationCenter.default.post(name: .toInfoDetailVC, object: nil, userInfo: dict)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}


//MARK: 游戏攻略cell
class HWStrategyGameCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {

    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        subviews{
            collectionView
        }
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 12
        layout.minimumInteritemSpacing = 12
        layout.scrollDirection = .horizontal
        layout.sectionInset = UIEdgeInsets(top: 0, left: 12*widthScale, bottom: 0, right: 0)
        layout.itemSize = CGSize(width: 185*widthScale, height: 150*widthScale)
        
        collectionView.collectionViewLayout = layout
        collectionView.backgroundColor = .clear
        collectionView.register(HWWuKongGameStrategyCell.self, forCellWithReuseIdentifier: HWWuKongGameStrategyCell.description())
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
        
        collectionView.leading(0).trailing(0).bottom(0).top(10*widthScale)
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HWWuKongGameStrategyCell.description(), for: indexPath) as? HWWuKongGameStrategyCell else { return UICollectionViewCell() }
//        cell.model = model?[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
